// Java 객체를 JS 객체로 변환
function forJS(oObject) {

    // HashMap -> Object
    if (oObject instanceof java.util.HashMap) {

        var oDatas = {};
        var aKeys = oObject['keySet']().toArray();

        for (var nIndex in aKeys) {

            var sKey = aKeys[nIndex];
            var sVal = oObject.get(sKey);

            oDatas[sKey] = forJS(sVal);

        }

        return oDatas;
	
	// ArrayList -> Array
    } else if (oObject instanceof java.util.ArrayList) {

        var aDatas = [];

        for (var i = 0, len = oObject.size(); i < len; i++)
            aDatas.push(forJS(oObject.get(i)));

        return aDatas;
	
	// Double/Float/Integer -> Number
    } else if (oObject instanceof java.lang.Double || oObject instanceof java.lang.Float || oObject instanceof java.lang.Integer) {

        return parseFloat(oObject);
		
	// String -> String
    } else if (oObject instanceof java.lang.String) {

        return String(oObject);

    }

    return oObject;

}

// JS 객체를 Java 객체로 변환
function forJava(oObject) {

    // Array -> ArrayList
    if (oObject.constructor === Array) {

        var oArray = new java.util.ArrayList();

        for (var i = 0, len = oObject.length; i < len; i++)
            oArray.add(forJava(oObject[i]));

        return oArray;
	
	// Object -> HashMap
    } else if (oObject.constructor === Object) {

        var oHash = new java.util.HashMap();

        for (var sKey in oObject)
            oHash.put(sKey, forJava(oObject[sKey]));

        return oHash;

    }

    return oObject;

}


function testMethod_1(oArrayList) {

    var oList = $A(forJS(oArrayList));
    var nRet = 0;

    oList.forEach(function(v) {
        nRet += v;
    });

    return forJava(nRet);

}

function testMethod_2(oHashMap) {

    var oObject = $H(forJS(oHashMap));
    var sRet = '';

    oObject.forEach(function(v, k) {
        sRet += v;
    });

    return forJava(sRet);

}

function testMethod_3(oArrayList) {

    var oList = $A(forJS(oArrayList));
    var aRet = [];

    oList.forEach(function(oItem) {

        var sStr = '';

        for (var k in oItem)
            sStr += k;

        aRet.push(sStr);

    });

    return forJava(aRet);

}

function testMethod_4(oHashMap) {

    var oRet;

    if (typeof oHashMap == 'undefined') {

        oRet = { 'foo' : 'DEFAULT' };

    } else {

        var oObject = $H(forJS(oHashMap));
        oObject.forEach(function(v, k, o) {
            o[k] *= 10;
        });

        oRet = oObject.$value();

    }

    return forJava(oRet);

}

function testMethod_5(oHashMap) {

    var oObject = $H(forJS(oHashMap));
    var aRet = [ '', 0 ];

    oObject.forEach(function(v, k) {
        aRet[0] += k;
		aRet[1] += v;
    });

    return forJava(aRet);

}

function testMethod_6(oArrayList) {
    return oArrayList;
}

function invokeCalculator(boResult) {
    var result = boResult / 2;
    return result;
}

function encodeDatas(oDatas) {

    var oObject = $H(forJS(oDatas));
    var sRet = '';

    oObject.forEach(function(v, k) {
        if (v instanceof java.util.HashMap) {

        } else {
            sRet = v.toUpperCase();
        }
    });

    return sRet;
}

function decodeDatas(oDatas) {
    var oObject = $H(forJS(oDatas));
    var sRet = '';

    oObject.forEach(function(v, k) {
        if (v instanceof java.util.HashMap) {

        } else {
            sRet = v.toLowerCase();
        }
    });

    return sRet;
}

function replaceValue(oDatas) {
    return oDatas.replace(/choco/, 'nhn');
}

function getBOList() {
    var groups = ['com.nhncorp.usf.core.service.HelloBO'];
    return forJava(groups);
}

function replaceParams(oHashMap) {
     var oRet;

    if (typeof oHashMap == 'undefined') {

        oRet = { 'foo' : 'DEFAULT' }

    } else {

        var oObject = forJS(oHashMap);
        for (var k in oObject) {
            if ( k == 'name')
                oObject[k] = 'choco';
            if (!isNaN(oObject[k]))
                oObject[k] *= 2;
        }

        oRet = oObject;

    }

    return forJava(oRet);
}

function testMethod_7(oHashMap) {

	var oRet;

	if (typeof oHashMap == 'undefined') {

		oRet = { 'foo' : 'DEFAULT' };

	} else {
		var oObject = $H(forJS(oHashMap));
		oRet = oObject.$value();
	}

	return forJava(oRet);
}

